package com.git.hui.yinshua.biz.rest.app.user;

import cn.hutool.core.lang.Validator;
import com.git.hui.yinshua.api.model.ExceptionUtil;
import com.git.hui.yinshua.api.model.ReqInfoContext;
import com.git.hui.yinshua.api.model.ResVo;
import com.git.hui.yinshua.api.model.Status;
import com.git.hui.yinshua.api.model.permission.Permission;
import com.git.hui.yinshua.api.model.permission.UserAccessRole;
import com.git.hui.yinshua.biz.common.service.ImageService;
import com.git.hui.yinshua.biz.common.wx.WxUserHelper;
import com.git.hui.yinshua.biz.rest.models.user.res.UserInfoDto;
import com.git.hui.yinshua.biz.rest.models.user.res.UserLoginDto;
import com.git.hui.yinshua.biz.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

/**
 * @author YiHui
 * @date 2024/6/11
 */
@Slf4j
@RestController
@RequestMapping(path = "app/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private ImageService imageService;

    @Autowired
    private WxUserHelper wxUserHelper;

    @GetMapping(path = "login")
    public ResVo<UserLoginDto> login(String code) {
        UserLoginDto user = userService.wxLogin(code);
        log.info("用户登录 -> {}, {}", code, user);
        return ResVo.ok(user);
    }

    @GetMapping(path = "info")
    @Permission(role = UserAccessRole.LOGIN)
    public ResVo<UserInfoDto> info() {
        UserInfoDto user = userService.getUserInfo(ReqInfoContext.getReqInfo().getUserId());
        return ResVo.ok(user);
    }

    /**
     * 更新头像
     *
     * @param request
     * @return
     */
    @GetMapping(path = "updateAvatar")
    @Permission(role = UserAccessRole.LOGIN)
    public ResVo<UserInfoDto> updateAvatar(MultipartHttpServletRequest request) {
        MultipartFile file = request.getFile("image");
        if (file == null) {
            throw ExceptionUtil.of(Status.StatusEnum.ILLEGAL_ARGUMENTS_MIXED, "缺少需要上传的图片");
        }

        String img = imageService.saveImg(request);
        UserInfoDto user = userService.updateUserInfo(ReqInfoContext.getReqInfo().getUserId(), img, null, null);
        return ResVo.ok(user);
    }

    /**
     * 用户昵称 or 手机号修改
     *
     * @param nick  昵称
     * @param phone 手机号
     * @return
     */
    @RequestMapping(path = "updateInfo")
    @Permission(role = UserAccessRole.LOGIN)
    public ResVo<UserInfoDto> updateUser(@RequestParam(name = "avatar", required = false) String avatar,
                                         @RequestParam(name = "nick", required = false) String nick,
                                         @RequestParam(name = "phone", required = false) String phone) {
        if (StringUtils.isBlank(nick) && StringUtils.isBlank(phone) && StringUtils.isBlank(avatar)) {
            return ResVo.ok((UserInfoDto) ReqInfoContext.getReqInfo().getUser());
        }

        if (StringUtils.isNotBlank(nick) && nick.length() > 32) {
            nick = nick.substring(0, 32);
        }

        if (StringUtils.isNotBlank(phone) && !Validator.isMobile(phone)) {
            throw ExceptionUtil.of(Status.StatusEnum.ILLEGAL_ARGUMENTS_MIXED, "手机号非法");
        }

        if (StringUtils.isNotBlank(avatar)) {
            avatar = imageService.saveImg(avatar);
        }

        UserInfoDto user = userService.updateUserInfo(ReqInfoContext.getReqInfo().getUserId(), avatar, nick, phone);
        return ResVo.ok(user);
    }

    /**
     * 登出
     *
     * @return
     */
    @RequestMapping("logout")
    @Permission(role = UserAccessRole.LOGIN)
    public ResVo<Boolean> logout() {
        return ResVo.ok(userService.logout());
    }


    @RequestMapping("phone")
    public String getPhoneNumber(String code) {
        return wxUserHelper.getPhone(code);
    }
}
